package com.tiantian.judge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import com.tiantian.common.core.domain.ResponseResult;
import com.tiantian.common.core.domain.page.TableDataInfo;
import com.tiantian.common.core.validate.AddGroup;
import com.tiantian.common.log.annotation.Log;
import com.tiantian.common.log.enums.BusinessType;
import com.tiantian.common.log.enums.OperatorType;
import com.tiantian.common.satoken.domain.controller.BaseController;
import com.tiantian.judge.domain.entity.Judger;
import com.tiantian.judge.domain.vo.JudgerVo;
import com.tiantian.judge.domain.vo.StaffVo;
import com.tiantian.judge.service.JudgerService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 判题人
 *
 * @author tiantian
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/judge")
public class SysJudgerController extends BaseController {

    private final JudgerService judgerService;

    /**
     * 获取判题人列表
     *
     * @param sysJudger 查询参数
     */
    @GetMapping("/judgerList")
    @SaCheckPermission("setting")
    public TableDataInfo getJudgerList(Judger sysJudger) {
        startPage();
        List<JudgerVo> sysJudgerList = judgerService.getJudgerList(sysJudger);
        return getDataTable(sysJudgerList);
    }

    /**
     * 获取候选人列表
     */
    @GetMapping("/candidate")
    @SaCheckPermission("setting")
    public ResponseResult<List<StaffVo>> getCandidate() {
        return ResponseResult.ok(judgerService.getStaffs());
    }

    /**
     * 添加判题人
     */
    @PostMapping("/judger")
    @SaCheckPermission("setting")
    @Log(businessType = BusinessType.JUDGE_SETTING, operatorType = OperatorType.ADMIN, isSaveResponseData = false)
    public ResponseResult<Void> addJudger(@RequestBody @Validated(AddGroup.class) Judger judger) {
        setCommonFieldInsert(judger);
        int rows = judgerService.setJudger(judger);
        return toAjax(rows);
    }

    /**
     * 删除判题人(物理删除)
     *
     * @param judgerId 判题人ID
     */
    @DeleteMapping("/delJudger/{judgerId}")
    @SaCheckPermission("setting")
    @Log(businessType = BusinessType.JUDGER_DELETE, operatorType = OperatorType.ADMIN, isSaveResponseData = false)
    public ResponseResult<Void> delJudger(@PathVariable String judgerId) {
        if (StrUtil.isEmpty(judgerId)) {
            return ResponseResult.fail("判题人ID不能为空");
        }
        int row = judgerService.deleteJudgerById(judgerId);
        // 异步发送邮件进行通知
        return toAjax(row);
    }
}
